const Service = require('./index')
const xmindModel = require('@/models/xmind.model')

class XmindService extends Service {
  /**
   * 思维导图分页查询
   * @param {*} pageNo
   * @param {*} pageSize
   * @param {*} searchKeywords
   */
  async findPage (pageNo = 1, pageSize = 20, searchKeywords) {
    try {
      const { list, total } = await xmindModel.findPage({
        pageNo,
        pageSize,
        conditions: {
          like: {
            'x_mind.name': searchKeywords,
            'user.nickname': searchKeywords
          }
        }
      })
      return this.skipPage(list, total, pageNo, pageSize)
    } catch (error) {
      throw new Error(error)
    }
  }

  async updateRecommend (xmindId, recommend) {
    try {
      if (![0, 1].includes(recommend)) {
        return {
          errorMessage: '不合法的 recommend 值'
        }
      }
      const exist = await xmindModel.exist(xmindId)
      if (!exist) {
        return {
          errorMessage: '思维导图不存在'
        }
      }
      await xmindModel.updateRecommend(xmindId, recommend)
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new XmindService()
